package org.copper.ui
{
	public class CHList extends BaseCList
	{
		public static const TO_CENTER:uint = 0;
		public static const TO_TOP:uint = 1;
		public static const TO_BUTTOM:uint = 2;
		
		private var m_halign:uint = TO_CENTER;
		private var m_dataWidth:Number = 0;
		
		private var m_avarageItmScrollSize:Number = 0;
		
		public function CHList()
		{
			super();
		}
		
		public function get halign():uint
		{
			return m_halign;
		}
		
		public function set halign(value:uint):void
		{
			if (value != m_halign)
			{
				switch(value)
				{
					case TO_CENTER:
						m_halign = TO_CENTER;
						break;
					case TO_TOP:
						m_halign = TO_TOP;
						break;
					case TO_BUTTOM:
						m_halign = TO_BUTTOM;
						break;
					default:
						m_halign = TO_CENTER;
						break;
				}
				forceUpdate();
			}
		}
		
		override protected function onDataProviderUpdated(dp:Object, pos:Number, container:CSprite):void
		{
			const dataLen:uint = dp.length;
			this.removeAllFromContainer();
			const w:Number = width;
			if (dataLen > 0)
			{
				var calculateY:Function = calculateYToCenter;
				switch(m_halign)
				{
					case TO_TOP:
						calculateY = calculateYToTop;
						break;
					case TO_BUTTOM:
						calculateY = calculateYToButtom;
						break;
				}
				
				var prevX:Number = 0;
				for(var i:uint = 0; i < dataLen; i++)
				{
					const renderer:CSprite = getRenderer();
					renderer.data = dp[i];
					renderer.x = prevX;
					calculateY(renderer);
					prevX += renderer.width;
					container.addChild(renderer);
				}
				m_dataWidth = prevX;
				
				const maxScrollLen:Number = 1 - (w / m_dataWidth);
				maxScroll = maxScrollLen;
				m_avarageItmScrollSize = maxScrollLen/dataLen;
			}
			else
			{
				m_avarageItmScrollSize = 0;
			}
			
			
			
			onScrollPositionUpdate(pos, pos);
		}
		
		protected function calculateYToTop(renderer:CSprite):void
		{
			renderer.y = 0;
		}
		
		
		protected function calculateYToCenter(renderer:CSprite):void
		{
			renderer.y = (this.height - renderer.height) * 0.5;
		}
		
		protected function calculateYToButtom(renderer:CSprite):void
		{
			renderer.y = this.height - renderer.height;
		}
		
		override protected function onScrollPositionUpdate(newPosition:Number, oldPosition:Number):void
		{
			this.scrollX = m_dataWidth * newPosition;
			this.render();
		}
		
		override protected function onTransform(offsetX:Number, offsetY:Number):void
		{
			if (offsetX != 0 && m_dataWidth != 0)
			{
				scrollPosition += offsetX / m_dataWidth;
			}
		}
		
		override public function get avarageItmScrollSize():Number
		{
			return m_avarageItmScrollSize;
		}
	}
}